/*
 * User: rant
 * Date: 02/01/2007
 *
 *
 */
package laMalinche.rete.nodes;

import laMalinche.rete.nodes.tailing.WorkingMemory;
import laMalinche.rete.nodes.tailing.WorkingMemoryEvent;
import laMalinche.rete.rule.Condition;

//import com.laMalinche.common.log.Log;
//import com.laMalinche.common.log.LogService;
import org.jetbrains.annotations.NotNull;
import com.google.inject.Inject;

/**
 *
 */
public abstract class BasicConditionNode<T extends Condition> extends BasicRelevantListener implements ConditionNode<T> {
//	private static final Log log = LogService.getLog(BasicConditionNode.class);
	private T condition;
      @Inject
	private WorkingMemory workingMemory;

	

	public void detach() {
		initializeListeners();
	}

	public int compareTo(ConditionNode o) {
		if (getClass().getCanonicalName().length() > o.getClass().getCanonicalName().length()) {
			return 1;
		} else {
            return -1;
        }
	}

	@Override
	protected void setConditionMet(Boolean met) {
		if (!compareBooleans(isConditionMet(), met)) {
			super.setConditionMet(met);
			adviseListeners(isConditionMet());
		}
	}

	@Override
	protected void informChangeRelevancy(final boolean relevant) {
		if (isConditionMet() != null || !isRelevant()) {
            return;
        }
		update();
	}

	@NotNull
	public T getCondition() {
		return condition;
	}

	protected boolean isConditionComplied() {
		return getCondition().isComplied();
	}

	public WorkingMemory getWorkingMemory() {
		return workingMemory;
	}

	public void workingMemoryChange(WorkingMemoryEvent event) {
		if (Boolean.FALSE.equals(event.isConditionMet())) {
			setConditionMet(false);
		} else {
			update();
		}
		// should be:
//		setConditionMet(event.isConditionMet());
//		update();
	}

	public void setCondition(T condition) {
		this.condition = condition;
	}
}